/*
 * @lc app=leetcode.cn id=794 lang=cpp
 *
 * [794] 有效的井字游戏
 */

// @lc code=start
class Solution
{
private:
    char isok(vector<string> &board)
    {
        //横着的三种
        for (int i = 0; i < 3; i++)
        {
            if (board[i] == "XXX")
            {
                return 'X';
            }
            else if (board[i] == "OOO")
            {
                return 'O';
            }
        }
        //竖
        for (int i = 0; i < 3; i++)
        {
            if (board[0][i] == board[1][i] && board[0][i] == board[2][i])
            {
                return board[0][i];
            }
        }
        //对角线
        if (board[0][0] == board[1][1] && board[0][0] == board[2][2])
        {
            return board[0][0];
        }
        if (board[2][0] == board[1][1] && board[2][0] == board[0][2])
        {
            return board[2][0];
        }
        return ' ';
    }

public:
    bool validTicTacToe(vector<string> &board)
    {
        //输入是三个字符串 横竖对角线成三个就可以
        //先放X再O
        //所以X的数量一定大于等于O
        //遍历board 记录X和O的个数 并查询是否有连续的三个一样
        int X = 0;
        int O = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (board[i][j] == 'X')
                {
                    X++;
                }
                else if (board[i][j] == 'O')
                {
                    O++;
                }
            }
        }
        //判断是否有连续的三个 一共有横竖6个以及两个对角线一共八种情况
        char res = isok(board);
        //如果返回是X那么 X=O+1 如果是O X=O
        if (res == 'X' && X == O + 1)
        {
            return true;
        }
        if (res == 'O' && X == O)
        {
            return true;
        }
        //如果没有三个一样的X或者O但是X>=O
        if (res == ' ')
        {
            if (X == O || X == O + 1)
            {
                return true;
            }
        }
        return false;
    }
};
// @lc code=end
